//
//  BDMyCouponsDetailViewController.m
//  iOS_beboy
//
//  Created by wen on 2017/9/21.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDMyCouponsDetailViewController.h"
#import "BDCouponValidationView.h"
#import "BDUserModel.h"

@interface BDMyCouponsDetailViewController ()

@end

@implementation BDMyCouponsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = _couponsModel.title;
    
    if (self.couponsModel) {
        [self layoutSubviews];
    }else{
        [self requestDatasource];
    }
}

- (void)requestDatasource{
    NSString *url = [NSString stringWithFormat:@"%@shopCouponPurchases/%@", KBDBaseURL, _code];
    NSString *headValid = [BDUserModel getHeaderValidation];
    
    RACSignal *signal = [[BDNetworkingServer v2_GET:url parameters:nil validation:headValid automaticProcessingError:YES] execute:nil];
    [signal subscribeNext:^(id  _Nullable x) {
        _couponsModel = [BDMyCouponsModel mj_objectWithKeyValues:x];
        [self layoutSubviews];
    }];
    [signal subscribeError:^(NSError * _Nullable error) {
        [MBManager showBriefAlert:ASLocalizedString(@"请求失败")clickHidden:YES clickBlock:nil];
    }];
}

- (void)layoutSubviews{
    
    UIView *customCouponInfoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kBDScreenWidth, 0)];
    customCouponInfoView.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = [UIFont systemFontOfSize:18];
    titleLabel.textColor = [UIColor colorWithHexString:@"#1c1c1c"];
    [customCouponInfoView addSubview:titleLabel];
    
    UIImageView *imageView = [[UIImageView alloc] init];
    [customCouponInfoView addSubview:imageView];
    
    UILabel *contnenLabel = [[UILabel alloc] init];
    contnenLabel.font = [UIFont systemFontOfSize:14];
    contnenLabel.textColor = [UIColor colorWithHexString:@"#767676"];
    contnenLabel.numberOfLines = 0;
    [customCouponInfoView addSubview:contnenLabel];
    
    UIView *dashLine = [[UIView alloc] initWithFrame:CGRectMake(15, 0, kBDScreenWidth - 30, 1)];
    [dashLine drawLineLength:5 lineSpacing:2 lineColor:[UIColor colorWithHexString:@"#e0e0e0"] verticality:NO];
    [customCouponInfoView addSubview:dashLine];
    
    UILabel *curentPriceLabel = [[UILabel alloc] init];
    curentPriceLabel.font = [UIFont systemFontOfSize:13];
    curentPriceLabel.textColor = [UIColor colorWithHexString:@"#767676"];
    [customCouponInfoView addSubview:curentPriceLabel];
    
    UILabel *oldPriceLabel = [[UILabel alloc] init];
    oldPriceLabel.font = [UIFont systemFontOfSize:15];
    oldPriceLabel.textColor = [UIColor colorWithHexString:@"#767676"];
    [customCouponInfoView addSubview:oldPriceLabel];
    
    UILabel *peopleNumberLabel = [[UILabel alloc] init];
    peopleNumberLabel.font = [UIFont systemFontOfSize:15];
    peopleNumberLabel.textColor = [UIColor colorWithHexString:@"#767676"];
    [customCouponInfoView addSubview:peopleNumberLabel];
    
    UIButton *qianggouButton = [UIButton buttonWithType:UIButtonTypeCustom];
    qianggouButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [customCouponInfoView addSubview:qianggouButton];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kBDScreenWidth, 0.5)];
    line.backgroundColor = [UIColor colorWithHexString:@"#e0e0e0"];
    [customCouponInfoView addSubview:line];
    
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.font = [UIFont systemFontOfSize:15];
    timeLabel.textColor = [UIColor colorWithHexString:@"#767676"];
    [customCouponInfoView addSubview:timeLabel];
    
        
    titleLabel.text = _couponsModel.title;
    [titleLabel sizeToFit];
    titleLabel.top = 10;
    titleLabel.centerX = customCouponInfoView.width / 2.0;
    
    [imageView sd_setImageWithURL:[NSURL URLWithString:_couponsModel.cover] placeholderImage:[UIImage imageNamed:@"shop_moren.jpg"]];
    imageView.size = CGSizeMake(101, 76);
    imageView.top = titleLabel.bottom + 24;
    imageView.left = 15;
    
    contnenLabel.text = _couponsModel.content;
    contnenLabel.width = kBDScreenWidth - imageView.right - 15 - 15;
    contnenLabel.height = [contnenLabel.text heightWithFont:contnenLabel.font width:contnenLabel.width];
    contnenLabel.left = imageView.right + 15;
    contnenLabel.top = imageView.top;
    
    dashLine.top = MAX(contnenLabel.bottom, imageView.bottom) + 20;
    
    NSString *string = [NSString stringWithFormat:ASLocalizedString(@"折扣价：¥%@"), [NSDecimalNumber decimalNumberWithString:_couponsModel.amount]];
    NSMutableAttributedString *attribuString = [[NSMutableAttributedString alloc] initWithString:string];
    NSRange range = [string rangeOfString:[NSString stringWithFormat:@"¥%@", [NSDecimalNumber decimalNumberWithString:_couponsModel.price]]];
    [attribuString setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"ff4f00"]} range:range];
    curentPriceLabel.attributedText = attribuString;
    [curentPriceLabel sizeToFit];
    curentPriceLabel.left = 15;
    curentPriceLabel.top = dashLine.bottom + 15;
    
    oldPriceLabel.text = [NSString stringWithFormat:ASLocalizedString(@"原价：¥%@"), [NSDecimalNumber decimalNumberWithString:_couponsModel.price]];
    [oldPriceLabel sizeToFit];
    oldPriceLabel.bottom = curentPriceLabel.bottom;
    oldPriceLabel.left = curentPriceLabel.right + 15;
    
    peopleNumberLabel.text = [NSString stringWithFormat:ASLocalizedString(@"推荐人数：%@人"), @(_couponsModel.rec_num)];
    [peopleNumberLabel sizeToFit];
    peopleNumberLabel.top = curentPriceLabel.bottom + 15;
    peopleNumberLabel.left = 15;
    
    line.top = peopleNumberLabel.bottom + 15;
        
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-mm-dd"];
    
    NSDate *start_at = [NSDate dateWithTimeIntervalSince1970:_couponsModel.start_at];
    NSString *startString = [formatter stringFromDate:start_at];
    
    NSDate *end_at = [NSDate dateWithTimeIntervalSince1970:_couponsModel.end_at];
    NSString *endString = [formatter stringFromDate:end_at];
    
    timeLabel.text = [NSString stringWithFormat:ASLocalizedString(@"有效期：%@ 至 %@"), startString, endString];
    [timeLabel sizeToFit];
    timeLabel.centerY = line.bottom + 20;
    timeLabel.left = 15;
    
    customCouponInfoView.height = line.bottom + 40;
    customCouponInfoView.top = kBDTopHeight + 10;
    [self.view addSubview:customCouponInfoView];
    
    if (_couponsModel.status == BDMyCouponsStatusNotUse) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.size = CGSizeMake(kBDScreenWidth - 30, 40);
        button.layer.masksToBounds = YES;
        button.layer.cornerRadius = 5;
        button.backgroundColor = KBDColorBlue;
        [button setTitle:ASLocalizedString(@"使用")forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            BDCouponValidationView *validationView = [BDCouponValidationView couponValidationViewWithCoupons:_couponsModel];
            [validationView show];
        }];
        button.centerX = kBDScreenWidth / 2.0;
        button.top = customCouponInfoView.bottom + 20;
        [self.view addSubview:button];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
